<?php

/**
 * Copyright              [HD框架] (C)2011-2012 houdunwang ，Inc. 
 * Encoding               UTF-8 
 * Version                $Id: configControl      2012-8-23 10:31:22
 * @author                向军
 * Link                   http://www.houdunwang.com       
 * E-mail                 houdunwangxj@gmail.com
 */
class configControl extends commonControl {

    function index() {
        $this->setconfig();
    }

    //编辑配置
    function setconfig() {
        $db = M("config");
        $tid = $db->group("tid")->field("tid")->all();
        $config = array();
        if ($tid) {
            foreach ($tid as $t) {
                $data = $db->where("tid={$t['tid']}")->order("tid asc,id asc")->all();
                $data = format_config($data);
                $config[$t['tid']] = $data;
            }
        }
        $this->assign("config", $config);
        $this->display("setconfig");
    }

    //重写配置文件
    function config_update($data = '') {
        //处理上传配置项
        $data = empty($data) ? $_POST : $data;
        if ($_FILES) {
            $files = $_FILES;
            $upload = new upload("config/data");
            $upfile = $upload->upload();
            foreach ($upfile as $file) {
                $data[$file['fieldname']] = $file['path'];
            }
        }

        $db = M("config");
        //修改配置项
        if ($data) {
            foreach ($data as $name => $v) {
                $db->where(array("name" => $name))->save(array("value" => $v));
            }
        }
        //重写配置文件
        $config = $db->field("name,value")->order("tid,id")->all();
        $data = array();
        foreach ($config as $name => $value) {
            $data[$name] = $value;
        }
        $str = "<?php \nif(!defined('PATH_HD'))exit;\nreturn " . var_export($data, true) . ";\n?>";
        if (file_put_contents("./config/base.php", $str)) {
            $this->success("修改配置成功");
        } else {
            $this->error("修改配置失败");
        }
    }

    //水印配置项
    function config_water_show() {
        $db = M("config");
        $waterconfig = $db->all("tid=3");
        $this->assign("waterconfig", format_config($waterconfig));
        $this->display();
    }



    //显示配置组
    function type() {
        $db = M("config_type");
        $type = $db->all();
        $this->assign("type", $type);
        $this->display();
    }

    //添加配置组
    function add_type_show() {
        $this->display();
    }

    //添加类型
    function add_type() {
        $db = M("config_type");
        if ($db->add()) {
            $this->success("添加配置类型成功", "type");
        } else {
            $this->error("添加配置类型失败", "type");
        }
    }

    //删除配置类
    function type_del() {
        $tid = $_GET['tid'];
        $db = M("config");
        if ($db->where("tid='$tid'")->find()) {
            $this->error("请先删除该配置组中的配置项");
        }
        if ($db->table("config_type")->del($tid)) {
            $this->success("配置项删除成功");
        } else {
            $this->erro("删除配置项失败");
        }
    }

    //编辑配置类
    function type_edit_show() {
        $tid = $_GET['tid'];
        $db = M("config_type");
        $this->assign("config_type", $db->find("tid='$tid'"));
        $this->display();
    }

    //修改配置组
    function type_update() {
        $db = M("config_type");
        if ($db->save()) {
            $this->success("修改配置组成功", "type");
        } else {
            $this->error("修改配置组失败", "type");
        }
    }

    //配置项管理
    function config() {
        $db = K("configView");
        $where = isset($_GET['tid']) ? "config.tid = '" . $_GET['tid'] . "'" : '';
        $config = $db->where($where)->order("config.tid asc,id asc")->all();
        $this->assign("config", $config);
        $this->display();
    }

    //显示添加配置项
    function add_config_show() {
        $db = M("config_type");
//        $tid = isset($_GET['tid'])?$_GET['tid']:FALSE;
//        $where='';
//        if($tid){
//            $where = "tid=$tid";
//        }
        $this->assign("config_type", $db->all());
        $this->display();
    }

    //添加配置项
    function add_config() {
        $db = M("config");
        if ($db->add()) {
            $this->success("添加配置成功", "");
        } else {
            $this->error("添加配置失败", "");
        }
    }

    //删除配置项
    function config_del() {
        $db = M("config");
        if ($db->del($_GET["id"])) {
            //重写配置文件
            $this->config_update();
            $this->success("删除配置项成功");
        } else {
            $this->error("删除配置项失败");
        }
    }

    //编辑配置项
    function config_edit_show() {
        $db = K("configView");
        $this->assign("config_type", $db->join()->table("config_type")->all());
        $this->assign("config", $db->find("id='{$_GET['id']}'"));
        $this->display();
    }

    function config_edit() {
        $db = M("config");
        if ($db->save()) {
            $this->success("修改配置项成功", "config");
        } else {
            $this->error("修改配置项失败", "config");
        }
    }

    //水印设置
}

?>
